package xdpm.nhom16.GUI;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import xdpm.nhom16.R;
import xdpm.nhom16.BUS.BUSProvider;
import xdpm.nhom16.BUS.DataProvider;
import xdpm.nhom16.BUS.SearchEngine;
import xdpm.nhom16.DTO.*;
import xdpm.nhom16.helper.Utilities;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ChiTietDiaDiemActivity extends MapActivity
{
	/** Called when the activity is first created. */
	private AutoCompleteTextView txtSearch;
	private ImageButton imageButtonSearch;
	private DuLieuDTO duLieuDTO;
	private SearchEngine searchEng;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		searchEng = new SearchEngine(DataProvider.getContext());
		
		setContentView(R.layout.chitietdiadiem);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_search);
		
		
		txtSearch = (AutoCompleteTextView) findViewById(R.id.edittextQuerySearch);
		
		Autocomplete auto = new Autocomplete(txtSearch, searchEng, this);
		auto.test();
		
		txtSearch.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN
						|| keyCode == KeyEvent.KEYCODE_ENTER)
				{
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(txtSearch.getApplicationWindowToken(), 0);
				}

				return false;
			}
		});
		
		
		
		imageButtonSearch = (ImageButton) findViewById(R.id.imageButtonSearch);
		imageButtonSearch.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				onClickSearchButton();
			}
		});
		

		MapView mapView = (MapView) findViewById(R.id.mapview);
		TextView txtTenDiaDiem = (TextView) findViewById(R.id.textChiTietTenDiaDiem);
		TextView txtDiaChi = (TextView) findViewById(R.id.textChiTietDiaChi);
		TextView txtChuThich = (TextView) findViewById(R.id.textChiTietChiThich);
		ImageView imgDichVu = (ImageView) findViewById(R.id.imageViewDichVu);
		

		
		Bundle extras = getIntent().getExtras();
		int iMaDuLieu = extras.getInt("Du lieu");
		duLieuDTO = BUSProvider.getDuLieuDTO(iMaDuLieu, this);
		
		imgDichVu.setImageBitmap(Utilities.getDichVuIcon(this)[duLieuDTO.getMaDichVu() -1]);
		txtTenDiaDiem.setText(BUSProvider.getTenDiaDiem(duLieuDTO.getMaTenDiaDiem(), this));
		txtDiaChi.setText(BUSProvider.getDiaChi(duLieuDTO, this));
		if (!duLieuDTO.getChuThich().equals(""))
		{
			txtChuThich.setText("\"" + duLieuDTO.getChuThich() + "\"");
		}
		
		mapView.setBuiltInZoomControls(true);

		Drawable drawing = this.getResources().getDrawable(R.drawable.balloon);
		IconOverlay iconOverlay = new IconOverlay(drawing, this);
		
		
		Double latitude = duLieuDTO.getViDo()*1e6;
		Double longitude = duLieuDTO.getKinhDo()*1e6;
		GeoPoint point = new GeoPoint(latitude.intValue(), longitude.intValue());
	
		OverlayItem item = new OverlayItem(point, BUSProvider.getTenDiaDiem(duLieuDTO.getMaTenDiaDiem(), this), BUSProvider.getDiaChi(duLieuDTO, this));
		
		iconOverlay.addOverlay(item);
		
		mapView.getOverlays().add(iconOverlay);
		
		System.out.println(latitude.intValue() + " " + longitude.intValue());
		
		MapController controller = mapView.getController();
		
		controller.setCenter(point);
		controller.animateTo(point);
		controller.setZoom(15);
		
		
//		imgBanDo.setImageDrawable(Utilities.LoadImageFromWeb(url));
		if (BUSProvider.checkIfHasMenu(duLieuDTO.getMaDuLieu(), this))	{
			Button btnMenu = (Button) findViewById(R.id.buttonXemMenu);
			btnMenu.setVisibility(View.VISIBLE);
			btnMenu.setOnClickListener(new OnClickListener() {
				
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					onClickMenubutton();
				}
			});
		}
	}

	public void onClickSearchButton()	{
		Utilities.onClickButtonSearch(txtSearch.getText().toString(), this);
	}
	public void onClickMenubutton()	{

		BUSProvider.onClickMenu(duLieuDTO.getMaDuLieu(),BUSProvider.getTenDiaDiem(duLieuDTO.getMaTenDiaDiem(), this), this);
	}
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}